<template>
  <view>
    <!-- 收货地址 -->
    <view class="iot-revice_address_row">
      <view class="iot-user_info_row">
        <view class="iot-user_info">
          <view>{{ address.userName }}</view>
          <view>{{ address.all }}</view>
        </view>
        <view class="iot-user_phone">{{ address.telNumber }}</view>
      </view>
    </view>
    <!-- 购物车内容 -->
    <view class="iot-cart_content">
      <view class="iot-cart_title">购物车</view>
      <view class="iot-cart_main">
        <view class="iot-cart_item" v-for="(item, index) in cart" :key="item.goods_id">
          <!-- 商品图片 -->

          <navigator class="iot-cart_img_wrap">
            <image mode="widthFix" :src="item.goods_small_logo"></image>
          </navigator>

          <!-- 商品信息 -->

          <view class="iot-cart_info_wrap">
            <view class="iot-goods_name">{{ item.goods_name }}</view>
            <view class="iot-goods_price_wrap">
              <view class="iot-goods_price">￥{{ item.goods_price }}</view>
              <view class="iot-cart_num_tool">
                <view class="iot-goods_num">X{{ item.num }}</view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 底部工具栏 -->
    <view class="iot-footer_tool">
      <!-- 总价格 -->
      <view class="iot-total_price_wrap">
        <view class="iot-total_price">
          合计:
          <text class="iot-total_price_text">￥{{ totalPrice }}</text>
        </view>
        <view>包含运费</view>
      </view>
      <!-- 结算 -->
      <view class="iot-order_pay_wrap" @tap="handleOrderPat">支付({{ totalNum }})</view>
    </view>
  </view>
</template>

<script>
import { request } from '../../request/index.js'
import {
  getSetting,
  chooseAddress,
  openSetting,
  showModal,
  showToast,
  requestPayment
} from '../../utils/async'
export default {
  data() {
    return {
      address: {
        userName: '',
        all: '',
        telNumber: ''
      },
      cart: [],
      totalPrice: 0,
      totalNum: 0
    }
  },
  onShow() {
    const address = uni.getStorageSync('address')
    let cart = uni.getStorageSync('cart') || []
    cart = cart.filter((v) => v.checked)

    this.address = address

    let totalPrice = 0
    let totalNum = 0
    cart.forEach((v) => {
      totalPrice += v.num * v.goods_price
      totalNum += v.num
    })

    this.cart, this.totalPrice, this.totalNum, this.address
  },
  methods: {
    async handleOrderPat() {
      try {
        const token = uni.getStorageSync('token')
        if (!token) {
          uni.navigateTo({
            url: '/pages/auth/index'
          })
          return
        }
        const order_price = this.totalPrice
        const consignee_addr = this.address.all
        const cart = this.cart
        let goods = []
        cart.forEach((v) =>
          goods.push({
            goods_id: v.goods_id,
            goods_number: v.num,
            goods_price: v.goods_price
          })
        )
        const orderparams = {
          order_price,
          consignee_addr,
          goods
        }
        const { order_number } = await request({
          url: 'my/orders/create',
          method: 'POST',
          data: orderparams
        })
        const { pay } = await request({
          url: 'my/orders/req_unifiedorder',
          method: 'POST',
          data: {
            order_number
          }
        })
        await requestPayment(pay)
        const res = await request({
          url: 'my/orders/chkOrder',
          method: 'POST',
          data: {
            order_number
          }
        })
        await showToast({
          title: '支付成功'
        })
        let newCart = uni.getStorageSync('cart')
        newCart = newCart.filter((v) => !v.checked)
        uni.setStorageSync('cart', newCart)
        uni.navigateTo({
          url: '/pages/order/index'
        })
      } catch (error) {
        await showToast({
          title: '支付失败'
        })
        console.log(error)
      }
    }
  }
}
</script>
<style>
@import './index.css';
</style>
